package com.ojoin.ojoin.eb_spider.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.openqa.selenium.Cookie;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ojoin.ojoin.eb_spider.service.CookieService;
import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.utils.LogUtils;
import com.ojoin.trade.common.utils.file.ObjectSaveFileUtils;
import com.ojoin.trade.common.web.service.DictService;
import com.ojoin.trade.supplyloan_core.constdef.IPlatformUrlDef;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlPlatformUser;

@Service("cookieServiceFile")
public class CookieServiceImplFile implements CookieService {

	private static Logger log = LoggerFactory.getLogger(CookieServiceImplFile.class);

	@Override
	public boolean saveCookies(String uid, Set<Cookie> cookie) throws IOException {
		Map<String, Set<Cookie>> cookies = new ConcurrentHashMap<String, Set<Cookie>>();
		cookies.put(uid, cookie);

		String filePath = getSaveCookieDirectory();

		File file = ObjectSaveFileUtils.getFile(filePath, "WebDriver.cookie_"
				+ uid);

		CommonUtils.serializedVo(file, cookies);

		LogUtils.info(log, "WebDriver保存路径为-->" + file.getAbsolutePath());

		return true;
	}

	@Override
	public Set<Cookie> getCookies(String uid) throws IOException {
		
		File file = getCookieFileByUser(uid);

		if (file.exists()) {

			LogUtils.info(log, "cookie 登陆  [%s] ", file.getAbsolutePath());

			Map<String, Set<Cookie>> str = CommonUtils.deSerializedVo(file);

			return str.get(uid+"");

		}else {
			return null ;
		}

	}
	
	
	
	public File getCookieFileByUser(String id) throws IOException {
		String filePath = getSaveCookieDirectory();

		File file = new File(filePath + File.separator + "WebDriver.cookie_"
				+ id);

		return file;
	}
	
	
	public static String getSaveCookieDirectory() {

		return System.getProperties().getProperty("user.home") + File.separator
				+ "Downloads" + File.separator + "webdriver";
	}

	@Override
	public void cookieKeeyOnline(List<TbSlPlatformUser> list) {
		// TODO Auto-generated method stub
		
	}


	
	
	
}
